//
//  TMShopHomeHeadView.m
//  国学商家管理
//
//  Created by Tao Li on 2018/6/26.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMShopHomeHeadView.h"
#import "SPToolSet.h"
#import "TMShopCourseModel.h"
#import "UIImage+TMimageCloor.h"
@implementation TMShopHomeHeadView

- (void)awakeFromNib {
    [super awakeFromNib];


}
- (void)setHeadArray:(NSArray *)headArray{
    _headArray = headArray;
    NSString *hascoll = _headArray[4];
    //TMShopListModel *model = _headArray[1];
    TMShopProveModel *pmodel = _headArray[2];
    [self displySC]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithBaseUrl:pmodel.logo]]];
        //然后就是添加照片语句，记得使用imageWithData:方法，不是imageWithName:。
        UIImage* resultImage = [UIImage imageWithData: imageData];
        if (resultImage != nil) {
            dispatch_async(dispatch_get_main_queue(), ^{
                //headView.headImageView.image = resultImage;
                self.courseImageView.image = [SPToolSet boxblurImage:resultImage withBlurNumber:0.8];
                UIImage *images = [UIImage clipImgWithName:resultImage];
                self.lingImage.image = images;
            });
        }
    });
    self.courseName.text = pmodel.title;
    self.whatchLabel.text = [NSString stringWithFormat:@"浏览人数：%ld", (long)pmodel.views];
        if ([hascoll integerValue] == 0) {
            self.collectBtn.selected = NO;
        }else{
            self.collectBtn.selected = YES;
        }
//    [self.collectBtn setTitle:[NSString stringWithFormat:@"%@人", hascoll] forState:UIControlStateNormal];
    self.addressLabel.text = [NSString stringWithFormat:@"地址：%@", pmodel.address];
    self.phoneLabel.text = [NSString stringWithFormat:@"电话：%@", pmodel.phone];
    self.linkManLabel.text = [NSString stringWithFormat:@"联系人：%@", pmodel.name];
    NSString *bussiness = @"";
    for (NSString *dic in pmodel.business) {
        bussiness = [NSString stringWithFormat:@"%@,%@", bussiness, dic];
    }
    if (bussiness.length >0) {
        self.mainbusinessLabel.text = [NSString stringWithFormat:@"主营业务：%@" ,[bussiness substringFromIndex:1]];
    }
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSlectedPhone)];
    [self.phoneLabel addGestureRecognizer:tap];
    self.phoneLabel.userInteractionEnabled = YES;
}

//- (void)layoutSubviews{
//    if (kWindowW == 320) {
//        self.courseImageView.frame = CGRectMake(12, 18, 110, 86);
//        self.courseName.frame = CGRectMake(110+12+15, 28, kWindowW - 110-12-15 -10, 16);
//        self.whatchLabel.frame = CGRectMake(110+12+15, 28+16+29, 100, 16);
//
//
//    }
//}
- (void)onSlectedPhone{
    NSString * phone = [self.phoneLabel.text stringByReplacingOccurrencesOfString:@"电话：" withString:@""];
    NSMutableString * string = [[NSMutableString alloc] initWithFormat:@"telprompt:%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];

}
- (void)displySC{
     TMShopProveModel *pmodel = _headArray[2];
    NSMutableArray *list = [NSMutableArray array];
    [list addObjectsFromArray:pmodel.business_license];
    [list addObjectsFromArray:pmodel.qualification_certificate];
    [self.checkSC setContentSize:CGSizeMake(105*list.count +6, 0)];
    self.checkSC.pagingEnabled = YES;
    for (int i = 0; i < list.count; i ++) {
        UIImageView *itemView = [[UIImageView alloc]initWithFrame:CGRectMake(12 +(99 +6) *i, 0, 99, self.checkSC.bounds.size.height)];
        itemView.tag = 100+i;
        [self.checkSC addSubview:itemView];
        [itemView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithBaseUrl:list[i]]] placeholderImage:kPlaceholder];
//        if (i < list.count) {
//             [itemView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithBaseUrl:list[i]]] placeholderImage:kPlaceholder];
//        }else{
//            itemView.image = [UIImage imageNamed:@"圆角矩形2"];
//            itemView.image = [SPToolSet boxblurImage:[UIImage imageNamed:@"tu"] withBlurNumber:0.2];
//        }
        //        itemView.image = [UIImage imageNamed:@"圆角矩形2"];
//        itemView.image = [SPToolSet boxblurImage:[UIImage imageNamed:@"tu"] withBlurNumber:0.2];
        
        itemView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
//        UILabel *label = [[UILabel alloc]init];
//        [itemView addSubview:label];
//       // BOOL hasVideo = i%2;
//        if (hasVideo == NO) {
//            [label mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.centerX.equalTo(itemView.mas_centerX);
//                make.centerY.equalTo(itemView.mas_centerY);
//                make.left.equalTo(itemView.mas_left).offset(5);
//                make.right.equalTo(itemView.mas_right).offset(5);
//            }];
//        }else{
//            [label mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.centerX.equalTo(itemView.mas_centerX);
//                make.top.equalTo(itemView.mas_top).offset(11); make.left.equalTo(itemView.mas_left).offset(5);
//                make.right.equalTo(itemView.mas_right).offset(5);
//            }];
//            UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//            [itemView addSubview:addBtn];
//            [addBtn setImage:[UIImage imageNamed:@"组4"] forState:UIControlStateNormal];
//            [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.centerX.equalTo(itemView.mas_centerX);
//                make.top.equalTo(label.mas_bottom).offset(2);
//                make.size.mas_equalTo(CGSizeMake(21, 21));
//
//            }];
//        }
//        label.text = @"调用商家认证时后台传的图";
//        label.textColor = [UIColor hx_colorWithHexRGBAString:@"FFFFFF"];
//        label.font = [UIFont systemFontOfSize:10];
//        label.numberOfLines = 2;
    }
}
- (IBAction)collectionAction:(UIButton *)sender {
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    sender.selected = !sender.selected;
    if (self.block ) {
        self.block(sender.selected);
    }
}
@end
